<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta
            name="viewport"
            content="width=device-width, initial-scale=1.0"
    />
    <meta
            http-equiv="X-UA-Compatible"
            content="ie=edge"
    />
    <link rel="stylesheet" href="/static/css/form.css"/>
    <title>Static Template</title>
</head>
<body>
<div class="container">
    <form action="javascript:;" class="login-form">
        <h2>欢迎使用每日健康助手</h2>
        <input type="text" id="class_num" placeholder="输入班级号码(6位)">
        <textarea name="form" cols="30" rows="10" placeholder="输入名单" id="name_list"></textarea>
        <button type="submit" class="checked">查询</button>
    </form>
</div>
</div>
</body>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
    const text = document.querySelector('#name_list')
    const button = document.querySelector('button')
    const class_num = document.querySelector('#class_num')
    button.addEventListener('click', () => {
        if (button.classList.contains('checked')) {
            var num = class_num.value
            var content = text.value
            if (!content || !num) {
                alert('有必填项未填')
            } else {

                $.ajax({
                    url: '/checkuser/',
                    data: {
                        'name_list': content,
                        'class_num': num,
                        csrfmiddlewaretoken: '{{ csrf_token }}'
                    },
                    type: 'post',
                    success: function (data) {
                        text.value = data.hint_list
                        button.innerHTML = '一键复制'
                        button.classList.remove('checked')
                        button.classList.add('copy')
                    }
                })
            }
        } else if (button.classList.contains('copy')) {
            text.select()
            document.execCommand('copy')
            button.innerHTML = '查询'
            button.classList.remove('copy')
            button.classList.add('checked')
            text.value = ''
            class_num.value = ''
            alert('复制成功')
        }
    })


</script>
</html>
